package com.glory.wxelectricitypda.activity;

import android.os.Handler;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.glory.wxelectricitypda.R;
import com.glory.wxelectricitypda.base.BaseDialogActivity;
import com.glory.wxelectricitypda.bean.BaseResponseBean;
import com.glory.wxelectricitypda.utils.NetUtils;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.OnClick;

public class AbortActivity extends BaseDialogActivity {


    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.etUser)
    FreeUI_ClearEditText etUser;
    @BindView(R.id.etLotId)
    FreeUI_ClearEditText etLotId;
    @BindView(R.id.etRemark)
    FreeUI_ClearEditText etRemark;
    @BindView(R.id.tvLotTitle)
    TextView tvLotTitle;

    private String actionType = "BYLOT";

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_abort;
    }

    @Override
    protected void initView() {
        freeUI_titleView.setTitle("退站");
        freeUI_titleView.setRightListener("提交", new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String userName = etUser.getText().toString();
                if (userName.isEmpty()) {
                    showToast("请输入操作人工号", false);
                    return;
                }
                requestUser(userName, new CallBack() {
                    @Override
                    public void success() {
                        String lotId = etLotId.getText().toString().trim();
                        if (lotId.length() == 0) {
                            showToast("请扫描生产批号", false);
                            return;
                        }
                        String remark = etRemark.getText().toString().trim();
                        if (remark.length() == 0) {
                            showToast("请输入退站说明", false);
                            return;
                        }
                        JSONObject map = new JSONObject();
                        try {
                            map.put("LOTID", lotId);
                            map.put("COMMENTS", remark);
                            map.put("ACTIONTYPE", actionType);

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        NetUtils.requestWithNormalCallBack("EUIT.EUITABORT", map, new FreeUI_DialogEntityCallBack<BaseResponseBean<?>>(
                                new TypeToken<BaseResponseBean<?>>() {
                                }.getType(), getSupportFragmentManager(), this) {
                            @Override
                            public void onSuccess(Response<BaseResponseBean<?>> response) {
                                if (response.body().isSuccess(getApplicationContext(), false)) {
                                    showToast("退站成功", true);
                                    new Handler().postDelayed(new Runnable() {
                                        @Override
                                        public void run() {
                                            AbortActivity.this.finish();
                                        }
                                    }, 2000);
                                }
                            }

                            @Override
                            public void onError(Response<BaseResponseBean<?>> response) {
                                super.onError(response);
                                loadError(response.getException(), "EUIT.EUITABORT");
                            }
                        });
                    }

                    @Override
                    public void fail() {
                        showToast("操作人工号验证失败，请重新输入", false);
                    }

                    @Override
                    public void error() {
                        showToast("连接不到服务器，请重试", false);
                    }
                });
            }
        });
    }


    @Override
    protected void initData() {
        etLotId.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (start == 0 && before == 0 && count > 1) {
                    etRemark.requestFocus();
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        return new FreeUI_AddViewEntity("header", llMain);
    }

    @OnClick({R.id.rbLot, R.id.rbWork})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rbLot:
                actionType = "BYLOT";
                tvLotTitle.setText("生产批号：");
                etLotId.setHint("请扫描生产批号");
                etLotId.setText("");
                break;
            case R.id.rbWork:
                actionType = "BYWO";
                tvLotTitle.setText("工单号　：");
                etLotId.setHint("请扫描工单号");
                etLotId.setText("");
                break;
        }
    }

}
